/*
 *  MW.cpp
 *  T3nsors
 *
 *  Created by Michael Barriault on 09-10-27.
 *  Copyright 2010 MikBarr Studios. All rights reserved.
 *
 */

#include <ctime>
#include <cstdlib>
#include "MW.h"
#include <unistd.h>
using namespace T3;

void MW::DefaultValues() {
	Badge->PrintStatus("Default values.");
	n = 10; // number of points in x - points in y/z will be chosen so d=dx=dy=dz
	s = 0.25; // s = dt/d
	epsilon = 0.05; // Dissipation coefficient
	K = 0.5; // Divergence constraint damping coefficient
	p = Vector(3); // Boundary type
	p(0) = p(1) = 0.;
	p(2) = 0.;
	c = 0.0; // Constraint violation
	Omega = 0.025; // Rotation rate of star
	//T = 2*M_PI/Omega; // one revolution
	T = 20.;
	I = 2./T; // Force-free constraint damping coefficient
	pole = 1; // Latitude on star as integer multiples of pi/2
 	id = (std::string)"work" + timecoord();
}

MW::MW() {
	DefaultValues();
}

MW::MW(Comm *iBadge, std::vector<std::string> argv) {
	Badge = iBadge->retain();
	Badge->PrintStatus("Allocating constants");
	DefaultValues();
	Args(argv);
    //	O = Domain(3,n,	-0.2,0.2,	-0.2,0.2,	-2.0,2.0); // Long box
	//  O = Domain(3,n,	0.0,1.0,	0.0,1.0,	0.0,1.0); // Cube
	O = Domain(3,n,	-0.5,0.5,	-1.0,1.0,	20.0,25.0); // Small-box
	//  O = Domain(3,n,	-0.1,0.1,	-0.5,0.5,	1.0,6.0); // Outside star
    //	O = Domain(3,n,	0.0,0.1,	0.0,1.0,	0.0,5.0); // Strip
	FORa O.p(a) = p(a);
	x = Vector(O,3,"x");
	stopit = false;
    
	PFOR(i,O(0)) FOR(j,O(1)) FOR(k,O(2)) {
		x(0)(i,j,k) = O.min(0) + i*O.d();
		x(1)(i,j,k) = O.min(1) + j*O.d();
		x(2)(i,j,k) = O.min(2) + k*O.d();
	}
    
	std::string cmd = mkdir(id);
	while ( system(cmd.c_str()) ) {
		for ( int i=id.size()-1; i>0; i-- ) {
			id[i]++;
			if ( id[i] < 58 ) break;
			else id[i] -= 10;
		}
        //		id = (std::string)str;
		cmd = mkdir(id);
	}
	/*string dir = (string)getcwd(NULL, 0) + "/" + id;
	if ( chdir(id.c_str()) != 0 ) {
		Badge->PrintStatus(id);
		exit(0);
	}*/
}

MW::~MW(void) {
	Badge->PrintStatus("Deallocating...");
	Badge->PrintStatus("", false);
	Badge->release();
}

